package ar.com.wholeconsulting.pacc.gui.utils.treetable;

import org.jdesktop.swingx.treetable.AbstractTreeTableModel;

public abstract class PACCAbstractTreeTableModel extends AbstractTreeTableModel {

    protected final TreeTableNode myroot;
    private String[] columnas;

    public PACCAbstractTreeTableModel(String[] columnas) {
        this.myroot = new TreeTableNode("root", "Root of the tree", null);
        this.columnas = columnas;
//
//		this.myroot.getChildren().add(new TreeTableNode("Empty Child 1", "This is an empty child"));
//
//		TreeTableNode subtree = new TreeTableNode("Sub Tree", "This is a subtree (it has children)");
//		subtree.getChildren().add(new TreeTableNode("EmptyChild 1, 1", "This is an empty child of a subtree"));
//		subtree.getChildren().add(new TreeTableNode("EmptyChild 1, 2", "This is an empty child of a subtree"));
//		this.myroot.getChildren().add(subtree);
//
//		this.myroot.getChildren().add(new TreeTableNode("Empty Child 2", "This is an empty child"));

    }

    @Override
    public int getColumnCount() {
        return columnas.length;
    }

    @Override
    public String getColumnName(int column) {
        return columnas[column];
    }

    @Override
    public abstract Object getValueAt(Object node, int column);

    @Override
    public Object getChild(Object node, int index) {
        TreeTableNode treenode = (TreeTableNode) node;
        return treenode.getChildren().get(index);
    }

    @Override
    public int getChildCount(Object parent) {
        TreeTableNode treenode = (TreeTableNode) parent;
        return treenode.getChildren().size();
    }

    @Override
    public int getIndexOfChild(Object parent, Object child) {
        TreeTableNode treenode = (TreeTableNode) parent;
        for (int i = 0; i > treenode.getChildren().size(); i++) {
            if (treenode.getChildren().get(i) == child) {
                return i;
            }
        }

        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public boolean isLeaf(Object node) {
        TreeTableNode treenode = (TreeTableNode) node;
        if (treenode.getChildren().size() > 0) {
            return false;
        }
        return true;
    }

    @Override
    public Object getRoot() {
        return this.myroot;
    }

    public void addToRoot(TreeTableNode node) {
        this.myroot.getChildren().add(node);
    }

    public void emptyModel(){
        this.myroot.getChildren().clear();
    }
}
